<template>
  <div class="block">
    <div style="padding: 0.625rem">
      <el-table
        v-loading="loading"
        :data="pageData"
        fit
        style="width: 100%"
        @sort-change="handleSortChange"
      >
        <el-table-column
          label="ID"
          prop="id"
          sortable="custom"
          align="center"
          width="80"
        >
          <template #default="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="用户名">
          <template #default="scope">
            <span>{{ scope.row.username }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作菜单">
          <template #default="scope">
            <span>{{ scope.row.title }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template #default="scope">
            <el-tag
              :type="scope.row.status == 'error' ? 'danger' : 'success'"
              effect="dark"
            >
              {{ scope.row.status == "success" ? "成功" : "失败" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="接口地址">
          <template #default="scope">
            <span>{{ scope.row.url }}</span>
          </template>
        </el-table-column>
        <!--        <el-table-column label="操作IP">-->
        <!--          <template #default="scope">-->
        <!--            <span>{{ scope.row.ip }}</span>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column label="IP归属">
          <template #default="scope">
            <span>{{ scope.row.ip_address }}</span>
          </template>
        </el-table-column>
        <el-table-column label="客户浏览器">
          <template #default="scope">
            <span>{{ scope.row.browser_type }}</span>
          </template>
        </el-table-column>
        <el-table-column label="系统" class-name="status-col">
          <template #default="scope">
            <span>{{ scope.row.system_type }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作时间">
          <template #default="scope">
            <span>{{ scope.row.created_at }}</span>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.p"
        v-model:limit="queryParams.limit"
        @pagination="handleQuery"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { operateLogLst } from "@/api/user";
import { operateLog, operateLogQuery } from "@/api/operateLog/types";
const loading = ref(false); //  加载状态
const queryParams = reactive<operateLogQuery>({
  p: 1,
  limit: 10,
});
const total = ref(0); // 数据总数
const pageData = ref<operateLog[]>(); // 用户分页数据
function handleQuery() {
  loading.value = true;
  operateLogLst(queryParams)
    .then(({ data }) => {
      pageData.value = data.items;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}
/** 排序 */
function handleSortChange(data: any) {
  const { prop, order } = data;
  if (prop === "id") {
    if (order === "ascending") {
      queryParams.sort = "+id";
    } else {
      queryParams.sort = "-id";
    }
    handleQuery();
  }
}

onMounted(() => {
  handleQuery();
});
</script>
